#include "adminpage.h"
#include "ui_adminpage.h"

AdminPage::AdminPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AdminPage)
    , m_ptrStumysql(nullptr)
{
    ui->setupUi(this);
    m_ptrStumysql = stumySql::getinstance();
    //m_ptrStumysql->init();//数据库的连接
    updateTable();
}
void AdminPage::updateTable()
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStringList l;
    l << "序号" << "用户名" << "密码" << "用户权限";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //每次选中会选中一行
    // ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//决定可不可以选多行,但要按住ctrl
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    auto cnt = m_ptrStumysql->getUserCnt();
    ui->lb_cnt->setText(QString("用户总数：%1").arg(cnt));
    QList<UserInfo> lStudents = m_ptrStumysql->getAllUser();
    ui->tableWidget->setRowCount(cnt);
    for (int i = 0; i < lStudents.size(); i++) {
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i)));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(lStudents[i].username));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(lStudents[i].password));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(lStudents[i].aut));
    }
}
AdminPage::~AdminPage()
{
    delete ui;
}
// void AdminPage::keyPressEvent(QKeyEvent *event)//处理键盘按键按下事件,是在主界面按下时触发的事件
// {
//     if(event->key()==Qt::Key_F6)//按下对应的按键key_F6,指的就是键盘上的F6,就会在对应界面实现css的样式
//     {
//         QFile f;
//         auto str=QCoreApplication::applicationDirPath();
//         f.setFileName(str+"//"+"stuqss.css");
//         f.open(QIODevice::ReadOnly);
//         QString strQss =f.readAll();
//         this->setStyleSheet(strQss);
//         m_dlgLogin.setStyleSheet(strQss);//css样式在登录界面实现
//     }
// }

void AdminPage::on_btn_add_clicked()
{
    m_dlg_addUser.setType(true);
    m_dlg_addUser.exec();
    updateTable();
}

void AdminPage::on_btn_del_clicked()
{
    int i = ui->tableWidget->currentRow();
    if (i >= 0) {
        QString strusername = ui->tableWidget->item(i, 1)->text();
        m_ptrStumysql->delUser(strusername);
        updateTable();
        QMessageBox::information(nullptr, "信息", "删除成功");
    }
}

void AdminPage::on_btn_update_clicked()
{
    UserInfo info;
    int i = ui->tableWidget->currentRow();
    if (i >= 0) {
        info.username = ui->tableWidget->item(i, 1)->text();
        info.password = ui->tableWidget->item(i, 2)->text();
        info.aut = ui->tableWidget->item(i, 3)->text();
        m_dlg_addUser.setType(false, info);
        m_dlg_addUser.exec();
    }
    updateTable();
}

void AdminPage::on_btn_search_clicked()
{
    QString strFilter = ui->le_search->text();
    if (strFilter.isEmpty()) {
        updateTable();
        return;
    }
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStringList l;
    l << "序号" << "用户名" << "密码" << "用户权限";
    ui->tableWidget->setHorizontalHeaderLabels(l);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //每次选中会选中一行
    // ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//决定可不可以选多行,但要按住ctrl
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //无法直接在界面修改
    auto cnt = m_ptrStumysql->getUserCnt();
    ui->lb_cnt->setText(QString("用户总数：%1").arg(cnt));
    QList<UserInfo> lStudents = m_ptrStumysql->getAllUser();
    ui->tableWidget->setRowCount(cnt);
    int index = 0;
    for (int i = 0; i < lStudents.size(); i++) {
        if (!lStudents[i].username.contains(strFilter)) {
            continue;
        }
        ui->tableWidget->setItem(index, 0, new QTableWidgetItem(QString::number(index + 1)));
        ui->tableWidget->setItem(index, 1, new QTableWidgetItem(lStudents[i].username));
        ui->tableWidget->setItem(index, 2, new QTableWidgetItem(lStudents[i].password));
        ui->tableWidget->setItem(index, 3, new QTableWidgetItem(lStudents[i].aut));
        index++;
    }
    ui->tableWidget->setRowCount(index);
}
